package com.crazy.crazyuserservice.controller;

import com.crazy.crazybusinessservice.controller.inner.BankCardBalanceInnerController;
import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.common.ResultUtils;
import com.crazy.crazymodel.model.dto.admin.AddAccountRequest;
import com.crazy.crazymodel.model.dto.admin.ReChargeRequest;
import com.crazy.crazymodel.model.dto.admin.UpdateCustomerInfoRequest;
import com.crazy.crazymodel.model.dto.trade.TradeRequest;
import com.crazy.crazymodel.model.vo.AddCustomerVO;
import com.crazy.crazymodel.model.vo.AllCustomerVO;
import com.crazy.crazymodel.model.vo.DelCardsVO;
import com.crazy.crazymodel.model.vo.GetMoneyVO;
import com.crazy.crazyuserservice.service.ClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;


@RestController
@RequestMapping("/account")
@Slf4j
@Api(tags = "开户模块相关接口")
public class ClientController {

    @Resource
    private ClientService clientService;
    @Resource
    private BankCardBalanceInnerController bankCardBalance;

    @PostMapping("/add")
    @ApiOperation(value = "开户登记")
    public BaseResponse<AddCustomerVO> addAccount(@RequestBody AddAccountRequest addAccountRequest){
        log.info("开户登记：{}", addAccountRequest);
        AddCustomerVO addCustomerVO = clientService.save(addAccountRequest);
        if (addCustomerVO != null)
            return ResultUtils.success(addCustomerVO);
        return ResultUtils.error(ErrorCode.PARAMS_ERROR);
    }

    @PostMapping("/recharge")
    @ApiOperation("模拟充值")
    public BaseResponse recharge(@RequestBody ReChargeRequest reChargeRequest){
        log.info("模拟充值：{}", reChargeRequest);
//        测试自用
//        BigDecimal money = clientService.recharge(reChargeRequest);
//        if (money != null)
//            return ResultUtils.success(null);
//        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        //使用内部接口封装
        TradeRequest tradeRequest = new TradeRequest();
        tradeRequest.setBankCardId(reChargeRequest.getBankCardNumber());
        tradeRequest.setAmount(reChargeRequest.getBalance());
        return bankCardBalance.revenue(tradeRequest);
    }

    @GetMapping("/getmoney")
    @ApiOperation("查询账号余额")
    public BaseResponse<GetMoneyVO> getMoney(@RequestParam String bankcard){
        log.info("查询账号余额：{}", bankcard);
        GetMoneyVO moneyVO = clientService.getMoney(bankcard);
        if (moneyVO != null)
            return ResultUtils.success(moneyVO);
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @GetMapping("/get")
    @ApiOperation("获取客户信息列表")
    public BaseResponse<AllCustomerVO> getCustomerInformation(@RequestParam Integer current, @RequestParam String name){
        log.info("模糊查询客户信息列表：{} {}", current, name);
        return ResultUtils.success(clientService.pageQuery(current, name));
    }

    @GetMapping("/getAll")
    @ApiOperation("获取全部客户列表")
    public BaseResponse<AllCustomerVO> getAllCustomer(){
        log.info("获取全部客户信息列表");
        return ResultUtils.success(clientService.getAllCustomer());
    }

    @PostMapping("/update")
    @ApiOperation("修改客户信息")
    public BaseResponse update(@RequestBody UpdateCustomerInfoRequest updateCustomerInfoRequest){
        log.info("修改客户信息：{}", updateCustomerInfoRequest);
        if (clientService.update(updateCustomerInfoRequest))
            return ResultUtils.success(null);
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @PutMapping("/delete")
    @ApiOperation("删除客户")
    public BaseResponse deleteCustomer(@RequestParam Integer id){
        log.info("删除客户信息：{}", id);
        if (clientService.deleteAccount(id))
            return ResultUtils.success(null);
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @PutMapping("/deletecard")
    @ApiOperation("删除客户银行卡")
    public BaseResponse deleteBankCard(@RequestParam String bankcard){
        log.info("删除银行卡信息：{}", bankcard);
        if (clientService.deleteBankCard(bankcard))
            return ResultUtils.success(null);
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }

    @GetMapping("/getCardDelete")
    @ApiOperation("查看客户银行卡是否能被删除")
    public BaseResponse<DelCardsVO> checkBankCardfordel(@RequestParam ArrayList<String> bankcards){
        log.info("确认删除银行卡信息：{}", bankcards);
        DelCardsVO delCardsVO = clientService.checkCarddel(bankcards);
        if (delCardsVO != null)
            return ResultUtils.success(delCardsVO);
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }
}
